package com.ds.admin.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ds.admin.event.User;
import com.ds.admin.event.UserEvent;
import com.ds.common.cache.redissonLock.RedissonLock;
import com.ds.common.cache.utils.RedisUtils;
import com.ds.common.web.response.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试树表Controller
 *
 * @author fs
 * @date 2021-07-26
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Slf4j
public class TStorageController {

    private final ApplicationEventPublisher publisher;


    /**
     * 测试spring事件
     *
     * @param uid 用户id
     */
    @GetMapping("/{uid}")
    public Result<User> method(@PathVariable Long uid) {

        ArrayList<User> list = new ArrayList<>();
        User sysMenu = new User();
        sysMenu.setId(IdWorker.getId());
        sysMenu.setName("张三");
        list.add(sysMenu);

        User sysMenu2 = new User();
        sysMenu2.setId(IdWorker.getId());
        sysMenu2.setName("张三2");
        list.add(sysMenu2);

        publisher.publishEvent(new UserEvent(this, new User(sysMenu.getId(), "张三", "武汉市")));

        //RedisUtils.set("userid:" + sysMenu.getId(), list,60);
        return Result.success(sysMenu);

    }

    @GetMapping("/kafka/{uid}")
    public Result<String> method2(@PathVariable Long uid) {
        String s = RedisUtils.getStr("userid:" + uid);
        log.info(s);
        List<User> list = RedisUtils.getList("userid:" + uid, User.class);
        log.info(JSONUtil.toJsonStr(list));
        return Result.success(s);

    }

    // @GetMapping("/send")
    // public R<String> method() {
    //
    // List<Message> list = new ArrayList<>();
    // for (int i = 0; i < 100; i++) {
    // list.add(MessageBuilder.withPayload("普通消息" + i).build());
    // }
    //
    //
    // rocketMQTemplate.syncSend("test", list);
    // //SendResult sendResult1 = rocketMQTemplate.syncSend("test", "this is a template
    // sync message");
    // //Message<String> msg = MessageBuilder.withPayload("延时消息测试").build();
    // //SendResult sendResult2 = rocketMQTemplate.syncSend("test", msg, 100, 3);
    // return R.ok();
    //
    // }

}
